package com.wzm.moviepic;

import java.util.ArrayList;
import java.util.List;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.umeng.analytics.MobclickAgent;
import com.umeng.update.UmengUpdateAgent;
import com.umeng.update.UmengUpdateListener;
import com.umeng.update.UpdateResponse;
import com.wzm.bean.MovieInfo;
import com.wzm.cache.ConfigCache;
import com.wzm.manager.AppConfig;
import com.wzm.manager.Tag;
import com.wzm.moviepic.slidingmenu.Main1Activity;
import com.wzm.service.UpdateService;
import com.wzm.utils.Logger;
import com.wzm.utils.UtilsTools;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class AppStart extends Activity {

	private ImageView load_icon = null;
	private ImageView load_intro = null;
	private Animation animation = null;
	private Context mContext = null;
	private FinalHttp fh = null;
	
	private TextView tv_ver = null;
	
 	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		mContext = this;
		
		MobclickAgent.onError(this);

		
		
		setContentView(R.layout.activity_appstart);
		load_icon = (ImageView)findViewById(R.id.loading_icon);
		load_intro = (ImageView)findViewById(R.id.loading_intro);
		tv_ver = (TextView)findViewById(R.id.textView1);
		tv_ver.setText("V "+Tag.VerName);
		//Intent upService = new Intent(this,UpdateService.class);
		//startService(upService);
		
		animation = AnimationUtils.loadAnimation(this, R.anim.fadeinn);
		load_icon.startAnimation(animation); 
		load_intro.startAnimation(animation); 
		tv_ver.startAnimation(animation);
		
		animation.setAnimationListener(new AnimationListener() {
			
			@Override
			public void onAnimationStart(Animation arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onAnimationRepeat(Animation arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onAnimationEnd(Animation arg0) {
				// TODO Auto-generated method stub								
//				Intent intent = new Intent(AppStart.this,Main1Activity.class);
//		    	startActivity(intent);
//		    	intent = null;
//		    	finish();
//		    	overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);
				getMovieList();
			}
		});		

	}
 	
 	
 	public void getMovieList()
 	{
 		fh = new FinalHttp();
 		final String baseid="-1";
        String cacheMovielist = ConfigCache.getUrlCache(AppConfig.MOVIE_URL+baseid);
        if(cacheMovielist==null)
        {
        	//POST数据      	
        	AjaxParams params = new AjaxParams();
    		params.put("baseid",baseid);
    		params.put("order", "0");
    		params.put("ver", Tag.VerNum);
    		fh.post(AppConfig.MOVIE_URL,params,new AjaxCallBack<Object>() {

				@Override
				public void onStart() {
					// TODO Auto-generated method stub
					super.onStart();
				}

				@Override
				public void onSuccess(Object t) {
					// TODO Auto-generated method stub
					super.onSuccess(t);
					// 保存更新时间
					UtilsTools.setTagString(mContext, Tag.DATE, UtilsTools.getDate());
					// 缓存JSON数据
					ConfigCache.setUrlCache((String)t, AppConfig.MOVIE_URL+baseid);		
					//跳转页面
					Intent intent = new Intent(AppStart.this,Main1Activity.class);
			    	startActivity(intent);
			    	overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);
			    	finish();
			    	intent = null;
				}

				@Override
				public void onFailure(Throwable t, String strMsg) {
					// TODO Auto-generated method stub
					super.onFailure(t, strMsg);
					
					Intent intent = new Intent(AppStart.this,Main1Activity.class);
			    	startActivity(intent);
			    	overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);
			    	finish();
			    	intent = null;
				}
    			
    		});
        }else{			
			Intent intent = new Intent(AppStart.this,Main1Activity.class);
	    	startActivity(intent);
	    	overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);
	    	finish();
	    	intent = null;
        }
 	}
	
 	
 	
 	
 	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		MobclickAgent.onPause(this);
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		MobclickAgent.onResume(this);
	}
 	
 	
	
}
